package com.dwxt.cdms.config;

import com.dwxt.cdms.api.common.utils.DateUtil;
import com.dwxt.cdms.constant.TaskMessageData;
import com.dwxt.cdms.entity.MedicationRemind;
import com.dwxt.cdms.entity.TaskMessage;
import com.dwxt.cdms.service.MedicationRemindService;
import com.dwxt.cdms.service.TaskMessageService;
import com.dwxt.cdms.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class InitRedisTask implements CommandLineRunner {

    @Autowired
    private TaskMessageService taskMessageService;

    @Autowired
    private MedicationRemindService medicationRemindService;

    /**
     * @param args:
     * @return void
     * @Description: 初始化消息配置
     * @author chendq
     * @date 2021/9/26
     */
    @Override
    public void run(String... args)  {
       initTaskMessage();
       initMedicationRemind();
    }


    public void initTaskMessage() {
        try {
            List<TaskMessage> list = taskMessageService.list();
            for (TaskMessage taskMessage : list) {
                String type = taskMessage.getType();
                String timeStr = taskMessage.getTime();
                if (TaskMessageData.TYPE_TIMING.equals(type)) {//定时
                    Date date = DateUtil.parseToDate(timeStr, "yyyy-MM-dd HH:mm:ss");
                    Long time = (date.getTime() - new Date().getTime()) / 1000;
                    if (time > 0) {
                        RedisUtil.set(String.valueOf(taskMessage.getId()), taskMessage.getType(), time);
                    }
                } else if (TaskMessageData.TYPE_LOOP.equals(type)) {//循环
                    String timeType = taskMessage.getTimeType();
                    if (TaskMessageData.DAY.equals(timeType)) {//每天
                        Long dayTime = TaskMessageData.getDayTime(timeStr);
                        RedisUtil.set(String.valueOf(taskMessage.getId()), taskMessage.getType(), dayTime);
                    } else if (TaskMessageData.WEEK.equals(timeType)) {//每周
                        Long weekTime = TaskMessageData.getWeekTime(timeStr);
                        RedisUtil.set(String.valueOf(taskMessage.getId()), taskMessage.getTitle(), weekTime);
                    } else if (TaskMessageData.MONTH.equals(timeType)) {//每月
                        Long monthTime = TaskMessageData.getMonthTime(timeStr);
                        RedisUtil.set(String.valueOf(taskMessage.getId()), taskMessage.getType(), monthTime);
                    }
                }
            }
        } catch (Exception e) {
            log.error("初始化消息管理失败。异常为："+e.getMessage());
        }
    }

    public void initMedicationRemind() {
        try {
            List<MedicationRemind> list = medicationRemindService.list();
            for (MedicationRemind medicationRemind : list) {
                medicationRemindService.setRedisValue(medicationRemind);
            }

        } catch (Exception e) {
            log.error("初始化用药提醒失败。异常为："+e.getMessage());
        }
    }
}
